package pw.caffe.common.net;

import java.io.UnsupportedEncodingException;
import java.nio.ByteOrder;

import org.apache.mina.core.buffer.IoBuffer;

public class NEXResponse {
	private Integer length;
	private int command;
	private IoBuffer ioMessage;

	public NEXResponse(int command, Object... args) {
		this.command = command;
		this.ioMessage = IoBuffer.allocate(255).setAutoExpand(true);
		this.ioMessage.order(ByteOrder.LITTLE_ENDIAN);
		this.ioMessage.putInt(0);
		this.ioMessage.putInt(command);
		for (int i = 0; i < args.length; i++) {
			Object arg = args[i];
			if (arg instanceof Byte) {
				this.writeByte((byte) arg);
			}
			if (arg instanceof byte[]) {
				this.writeByteArray((byte[]) arg);
			}
			if (arg instanceof Short) {
				this.writeShort((short) arg);
			}
			if (arg instanceof short[]) {
				this.writeShortArray((short[]) arg);
			}
			if (arg instanceof Integer) {
				this.writeInt((int) arg);
			}
			if (arg instanceof int[]) {
				this.writeIntArray((int[]) arg);
			}
			if (arg instanceof Double) {
				this.writeDouble((double) arg);
			}
			if (arg instanceof double[]) {
				this.writeDoubleArray((double[]) arg);
			}
			if (arg instanceof Float) {
				this.writeFloat((float) arg);
			}
			if (arg instanceof float[]) {
				this.writeFloatArray((float[]) arg);
			}
			if (arg instanceof Long) {
				this.writeLong((long) arg);
			}
			if (arg instanceof long[]) {
				this.writeLongArray((long[]) arg);
			}
			if (arg instanceof String) {
				this.writeString((String) arg);
			}
			if (arg instanceof String[]) {
				this.writeStringArray((String[]) arg);
			}
		}
	}

	public void setToSend() {
		this.ioMessage.flip();
		this.length = this.ioMessage.limit();
		this.ioMessage.putInt(length);
		this.ioMessage.position(0);
	}

	public int getCommand() {
		return command;
	}

	public void setCommand(int command) {
		this.command = command;
	}

	public Integer getLength() {
		return length;
	}

	public IoBuffer getIoMessage() {
		return ioMessage;
	}

	public void setIoMessage(IoBuffer ioMessage) {
		this.ioMessage = ioMessage;
	}

	public final void writeByte(byte value) {
		ioMessage.put(value);
	}

	public final void writeByteArray(byte[] data) {
		if (data == null) {
			ioMessage.putShort((short) 0);
		} else {
			ioMessage.putShort((short) data.length);
			ioMessage.put(data);
		}
	}

	public final void writeShort(short value) {
		ioMessage.putShort(value);
	}

	public final void writeShortArray(short[] data) {
		if (data == null) {
			ioMessage.putShort((short) 0);
		} else {
			ioMessage.putShort((short) data.length);
			for (int i = 0; i < data.length; i++)
				ioMessage.putShort(data[i]);
		}
	}

	public final void writeInt(int value) {
		ioMessage.putInt(value);
	}

	public final void writeIntArray(int[] data) {
		if (data == null) {
			ioMessage.putShort((short) 0);
		} else {
			ioMessage.putShort((short) data.length);
			for (int i = 0; i < data.length; i++)
				ioMessage.putInt(data[i]);
		}
	}

	public final void writeDouble(double value) {
		ioMessage.putDouble(value);
	}

	public final void writeDoubleArray(double[] data) {
		if (data == null) {
			ioMessage.putShort((short) 0);
		} else {
			ioMessage.putShort((short) data.length);
			for (int i = 0; i < data.length; i++)
				ioMessage.putDouble(data[i]);
		}
	}

	public final void writeFloat(float value) {
		ioMessage.putFloat(value);
	}

	public final void writeFloatArray(float[] data) {
		if (data == null) {
			ioMessage.putShort((short) 0);
		} else {
			ioMessage.putShort((short) data.length);
			for (int i = 0; i < data.length; i++)
				ioMessage.putFloat(data[i]);
		}
	}

	public final void writeLong(long value) {
		ioMessage.putLong(value);
	}

	public final void writeLongArray(long[] data) {
		if (data == null) {
			ioMessage.putShort((short) 0);
		} else {
			ioMessage.putShort((short) data.length);
			for (int i = 0; i < data.length; i++)
				ioMessage.putLong(data[i]);
		}
	}

	public final void writeString(String value) {
		if (value == null) {
			ioMessage.putShort((short) 0);
		} else {
			byte[] textByte = null;
			try {
				textByte = value.getBytes("UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			writeByteArray(textByte);
		}
	}

	public final void writeStringArray(String[] data) {
		if (data == null) {
			ioMessage.putShort((short) 0);
		} else {
			ioMessage.putShort((short) data.length);
			for (int i = 0; i < data.length; i++)
				writeString(data[i]);
		}
	}
}
